package cn.edu.zhku.citrusmonitor.illness.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;

import com.mysql.jdbc.Connection;

import cn.edu.zhku.citrusmonitor.domain.TbIllness;
import cn.edu.zhku.citrusmonitor.domain.TbUser;
import cn.edu.zhku.citrusmonitor.illness.dao.IllnessDao;
import cn.edu.zhku.citrusmonitor.illness.dao.IllnessDaoImpl;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.UUIDUtils;
import cn.edu.zhku.citrusmonitor.util.xy.PageBean;
import net.sf.json.JSONObject;

public class IllnessServiceImpl implements IllnessService {

	IllnessDao ID = new IllnessDaoImpl();
	
	@Override
	public JSONObject modifyIllness(TbIllness illness,TbUser user) {
			try {
				if(ID.modifyIllnessByIllnessBean(illness)!=0) {
					return JSONObject.fromObject(new Message("1","修改成功"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			System.out.println("xiugaishibai");
			return JSONObject.fromObject(new Message("2","修改失败"));
	}

	@Override
	public JSONObject addIllness(TbIllness illness, TbUser user) {
		illness.setIllnessId(UUIDUtils.getId());
		try {
			if(ID.addIllnwssByIllnessBean(illness,user) != 0) {			
				return JSONObject.fromObject(new Message("1", "添加成功"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return JSONObject.fromObject(new Message("2", "添加失败"));
	}

	@Override
	public JSONObject removeIllness(TbIllness illness, TbUser user) {
			try {
				if(ID.removeIllnessByIllnessId(illness.getIllnessId()) != 0) {
					return JSONObject.fromObject(new Message("1", "删除成功"));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return JSONObject.fromObject(new Message("2", "删除失败"));
		
	}

	@Override
	public List<TbIllness> getIllness(String illnessId) {
		List<TbIllness> list = null;
		try {
			list = ID.getIllnessById(illnessId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public PageBean<TbIllness> findIllnessPageBean(Integer currentPage, Integer pageSize, TbIllness illness) {
		Long count = (long) 0;
		List<TbIllness> list = null;
		PageBean<TbIllness> pbn = new PageBean<>();
		try {
			count = ID.getCount();
			list = ID.getIllnessBeanList(currentPage, pageSize, illness);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);
			pbn.setPageSize(pageSize);
			pbn.setCount(count);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbn;
	}

	@Override
	public boolean isVipUser(TbUser user) {
		if(user == null) {
			return false;
		}else {
			if("vip".equals(user.getRole())) {
				return true;
			}else {
				return false;
			}
		}
	}

	@Override
	public PageBean<TbIllness> findAllIllnessDetail(Integer currentPage, Integer pageSize) {
		long count = 0;
		List<TbIllness> list = null;
		PageBean<TbIllness> pbn = new PageBean<>();
		try {
			count = ID.getCount();
			list = ID.getIllnessDetailBeanList(currentPage, pageSize);
			pbn.setList(list);
			pbn.setCurrentPage(currentPage);//当前页
			pbn.setPageSize(pageSize);		//每页条数
			pbn.setCount(count);;		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pbn;
	}

	@Override
	public PageBean<TbIllness> findAllIllnessDetailByWord(Integer currentPage, Integer pageSize, String word) {
		List<TbIllness> list = null;
		Long count = null;
		PageBean<TbIllness> pbn = new PageBean<>();
		pbn.setCurrentPage(currentPage);//当前页
		pbn.setPageSize(pageSize);		//每页条数
		if(word != "") {
			try {
				count  = ID.searchPestCountByWord(word);
				list = ID.searchPestByWord(currentPage, pageSize, word);
			}catch (SQLException e) {
				e.printStackTrace();
			}
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		}else {
			count = (long) 0;
			pbn.setList(list);
			pbn.setCount(count);		//总记录数
			pbn.setTotalPage(pbn.getTotalPage());	//在PageSIze下，总共页数
		}
		return pbn;
	}

	@Override
	public JSONObject delSelect(String[] ids) {
		Connection conn = null;
		try {
			if(ID.delSelect(ids).length > 0) {
				System.out.println("-ID.delSelect(ids).length-");
				System.out.println(ID.delSelect(ids).length);
				return JSONObject.fromObject(new Message("1","删除成功"));
			}
		} catch (SQLException e) {
			try {
				DbUtils.rollback(conn);	//进行事务回滚
				} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally {
			try {
				DbUtils.close(conn);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return JSONObject.fromObject(new Message("2","删除失败"));
	}
}
